package com.gzx.plugin.data.wxjk.sync.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gzx.plugin.data.wxjk.sync.dao.*;
import com.gzx.plugin.data.wxjk.sync.pojo.*;
import com.gzx.plugin.data.wxjk.sync.service.SssjService;
import com.gzx.plugin.data.wxjk.sync.utils.AesSupplierUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
@DS("data")
public class SssjServiceImpl implements SssjService {

    @Value("${aes.wxjkKey}")
    private String encryptByKey;

    @Autowired
    private SssjDao sssjDao;

    @Autowired
    private CorporateAccountConfigDao corporateAccountConfigDao;

    @Autowired
    private OverdueInfoDao overdueInfoDao;

    @Autowired
    private CleanLoanInfoDao cleanLoanInfoDao;

    @Autowired
    private CompenDao compenDao;
    @Autowired
    private RepaymentDao repaymentDao;

    @Autowired
    private PreRepaymentDao preRepaymentDao;

    @Autowired
    private FileDao fileDao;

    @Autowired
    private RepayPlanDao repayPlanDao;

    @Autowired
    private PreRepayPlanDao preRepayPlanDao;

    @Autowired
    private ContactInfoDao contactInfoDao;


    @Override
    @Transactional
    public Boolean save(String req) {

        Boolean flag = false;
        try {
            JSONObject reqData = JSON.parseObject(req);
            String data = reqData.getString("encryptData");
            //案件id
            Integer businessId = reqData.getInteger("businessId");

            //解密
            String customerData = AesSupplierUtil.aesDecrypt(data, encryptByKey);

            SssjCustomerDto ssjCustomerDto = JSON.parseObject(customerData, SssjCustomerDto.class);


            //对公还款账号信息对象
            List<SssjCorporateAccount> sssjCorporateAccountList = ssjCustomerDto.getCorporateAccountConfigDtos();
            if (!CollectionUtils.isEmpty(sssjCorporateAccountList)) {
                for (SssjCorporateAccount sssjCorporateAccount : sssjCorporateAccountList) {
                    sssjCorporateAccount.setBusinessId(businessId);
                }
                corporateAccountConfigDao.batchSave(sssjCorporateAccountList);
            }

            //逾期信息
            List<SssjOverdueInfo> sssjOverdueInfoList = ssjCustomerDto.getOverdueInfoDtos();
            if (!CollectionUtils.isEmpty(sssjOverdueInfoList)) {
                for (SssjOverdueInfo sssjOverdueInfo : sssjOverdueInfoList) {
                    sssjOverdueInfo.setBusinessId(businessId);
                }
                overdueInfoDao.batchSave(sssjOverdueInfoList);
            }


            //清贷信息
            List<SssjCleanLoanInfo> sssjCleanLoanInfoList = ssjCustomerDto.getCleanLoanInfoDtos();
            if (!CollectionUtils.isEmpty(sssjCleanLoanInfoList)) {
                for (SssjCleanLoanInfo sssjCleanLoanInfo : sssjCleanLoanInfoList) {
                    sssjCleanLoanInfo.setBusinessId(businessId);
                }
                cleanLoanInfoDao.batchSave(sssjCleanLoanInfoList);
            }

            //担保代偿信息
            List<SssjCompen> sssjCompenList = ssjCustomerDto.getCompenDtos();
            if (!CollectionUtils.isEmpty(sssjCompenList)) {
                for (SssjCompen sssjCompen : sssjCompenList) {
                    sssjCompen.setBusinessId(businessId);
                }
                compenDao.batchSave(sssjCompenList);
            }


            //担保代偿信息
            List<SssjRepayment> sssjRepaymentList = ssjCustomerDto.getRepaymentDtos();
            if (!CollectionUtils.isEmpty(sssjRepaymentList)) {
                for (SssjRepayment sssjRepayment : sssjRepaymentList) {
                    sssjRepayment.setBusinessId(businessId);
                }
                repaymentDao.batchSave(sssjRepaymentList);
            }


            //其他订单还款信息（前置费用）
            List<SssjPreRepayment> sssjPreRepaymentList = ssjCustomerDto.getPreRepaymentDtos();
            if (!CollectionUtils.isEmpty(sssjPreRepaymentList)) {
                for (SssjPreRepayment sssjPreRepayment : sssjPreRepaymentList) {
                    sssjPreRepayment.setBusinessId(businessId);
                }
                preRepaymentDao.batchSave(sssjPreRepaymentList);
            }

            //影像件
            List<SssjFile> sssjFileList = ssjCustomerDto.getFileDtos();
            if (!CollectionUtils.isEmpty(sssjFileList)) {
                for (SssjFile sssjFile : sssjFileList) {
                    sssjFile.setBusinessId(businessId);
                }
                fileDao.batchSave(sssjFileList);
            }

            //主订单还款计划表
            List<SssjRepayPlan> sssjRepayPlanList = ssjCustomerDto.getRepayPlanDtos();
            if (!CollectionUtils.isEmpty(sssjRepayPlanList)) {
                for (SssjRepayPlan sssjRepayPlan : sssjRepayPlanList) {
                    sssjRepayPlan.setBusinessId(businessId);
                }
                repayPlanDao.batchSave(sssjRepayPlanList);
            }


            //前置订单还款计划表
            List<SssjPreRepayPlan> sssjPreRepayPlanList = ssjCustomerDto.getPreRepayPlanDtos();
            if (!CollectionUtils.isEmpty(sssjPreRepayPlanList)) {
                for (SssjPreRepayPlan sssjPreRepayPlan : sssjPreRepayPlanList) {
                    sssjPreRepayPlan.setBusinessId(businessId);
                }
                preRepayPlanDao.batchSave(sssjPreRepayPlanList);
            }

            //联系人信息
            List<SssjContactInfo> sssjContactInfoList = ssjCustomerDto.getContactInfos();
            if (!CollectionUtils.isEmpty(sssjContactInfoList)) {
                for (SssjContactInfo sssjContactInfo : sssjContactInfoList) {
                    sssjContactInfo.setBusinessId(businessId);
                }
                contactInfoDao.batchSave(sssjContactInfoList);
            }


            //诉讼数据
            sssjDao.insertSelective(ssjCustomerDto);
            flag = true;
        } catch (Exception e) {
            log.error("诉讼数据推送接口保存失败");
            throw new RuntimeException(e);
        }


        return flag;
    }
}
